/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pa165.languageschoolpersistence.DAOImpl;

import com.pa165.languageschoolpersistence.DAO.CourseDAO;
import com.pa165.languageschoolpersistence.Exceptions.IllegalArgumentPersistenceException;
import com.pa165.languageschoolpersistence.entities.Course;
import com.pa165.languageschoolpersistence.entities.LanguageSpoken;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;

/**
 *
 * @author lexpenz
 */
@Repository("courseDAO")
public class CourseDAOImpl implements CourseDAO {

    private EntityManager em;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    public Course add(Course course) throws IllegalArgumentPersistenceException {
        if (course == null) {
            throw new IllegalArgumentPersistenceException("Method CourceDAOImpl.add call with Null argument student");
        }
        if (course.getName() == null || course.getDifficulty() == null || course.getLanguage() == null) {
            throw new IllegalArgumentPersistenceException("Method CourceDAOImpl.add call with Null field of course");
        }
        if (this.findByName(course.getName()).size() > 0) {
            throw new IllegalArgumentPersistenceException("Method CourceDAOImpl.add : course with name " + course.getName() + " already exists");
        }
        em.persist(course);
        return course;
    }

    public void remove(Course course) throws IllegalArgumentPersistenceException {
        if ((course == null) || (course.getId() == null)) {
            throw new IllegalArgumentPersistenceException("Method CourseDAOImpl.remove call with Null argument student or its id");
        }
        Course courseToRemove = em.find(Course.class, course.getId());
        em.remove(courseToRemove);
    }

    public Course modify(Course course) throws IllegalArgumentPersistenceException  {
        if ((course == null) || (course.getId() == null)) {
            throw new IllegalArgumentPersistenceException("Method CourseDAOImpl.modify call with Null argument student or its id");
        }
        if (course.getName() == null || course.getDifficulty() == null || course.getLanguage() == null) {
            throw new IllegalArgumentPersistenceException("Method CourceDAOImpl.modify call with Null field of course");
        }
        List<Course> list = this.findByName(course.getName());
        if (list.size() > 1) {

            throw new IllegalArgumentPersistenceException("Method CourceDAOImpl.modify : course with name " + course.getName() + " already exists");
        }
        em.merge(course);
        return course;
    }

    public Course get(Long id) throws IllegalArgumentPersistenceException {
        if (id == null) {
            throw new IllegalArgumentPersistenceException("Method CourseDAOImpl.get call with Null argument id");
        }
        Course course = em.find(Course.class, id);
        return course;
    }

    public List<Course> getAll() {
        List<Course> result = null;
        result = em.createQuery("from Course", Course.class).getResultList();
        return result;
    }

    public List<Course> findByLanguage(LanguageSpoken language) throws IllegalArgumentPersistenceException {
        if (language == null) {
            throw new IllegalArgumentPersistenceException("Method CourseDAOImpl.findByLecture call with Null argument lecture or id:");
        }
        List<Course> result;
        result = em.createQuery("from Course course where course.languageSpoken=:language").setParameter("language", language).getResultList();
        return result;
    }

    public List<Course> findByName(String name) throws IllegalArgumentPersistenceException {
        if (name == null) {
            throw new IllegalArgumentPersistenceException("Method CourseDAOImpl.get call with Null argument id");
        }
        List<Course> result;
        result = em.createQuery("from Course course where course.name=:name").setParameter("name", name).getResultList();
        return result;
    }
}
